var ctx;
var companyName = "";

function setCtx(ctx){
	this.ctx = ctx;
}

//判断值是否为空
function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//条件搜索用户列表
function getCompanyList(){
	companyName = ifNull($("#companyName").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(companyName == $("#companyName").attr("placeholder")){
		companyName = "";
	}
	
	loadContent("/operationcompany/companyList?pNo=1&companyName="+ companyName + "&pSize="+ pSize);
	
}

//跳转到指定页面
function gotoCompanyPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	companyName = ifNull($("#companyName").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(companyName == $("#companyName").attr("placeholder")){
		companyName = "";
	}
	
	loadContent("/operationcompany/companyList?pNo="+ page +"&companyName="+ companyName + "&pSize="+ pSize);
}

//根据左右键跳转页面
function gotoPage(page){
	companyName = ifNull($("#companyName").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(companyName == $("#companyName").attr("placeholder")){
		companyName = "";
	}
	
	loadContent("/operationcompany/companyList?pNo="+ page +"&companyName="+ companyName + "&pSize="+ pSize);
}

//新增或编辑用户(1:新增 2:编辑)
function saveOrUpdate (id) {
	if(id != null){
		loadContent("/operationcompany/editcompany?id="+id+"");
	}else{
		loadContent("/operationcompany/editcompany?id=0");
	}
	
}

//全选和反选
function selectAll(target){    
  if(target.checked){    
      $("#tableTbody :checkbox").attr("checked", true); 
      $(target).parents("thead").siblings("tbody").children().children().each(function(){
  	    $(this).css("background","#D3F0F1");
     });
  }else{    
      $("#tableTbody :checkbox").attr("checked", false); 
      $(target).parents("thead").siblings("tbody").children().children().each(function(){
  	    $(this).css("background","");
     });
  }    
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}


//选择或取消选中当前记录
function selectOne(id,target){
  if(target.checked){    
      $("#cell" + id).attr("checked", true);   
      $(target).parents("tr").children().each(function(){
  	    $(this).css("background","#D3F0F1");
     });
  }else{    
      $("#cell" + id).attr("checked", false); 
      $(target).parents("tr").children().each(function(){
  	    $(this).css("background","");
     });
  } 
}

//删除多个记录
function deleteByIds(){
	if($("img[name='companyChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
	    $("#tableTbody img[name='companyChecked'][src*=honggou]").each(function(){ 
	    	delIds.push($(this).attr("class")); 
	    }); 
		layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
		    //do something
			$.post(ctx+"/operationcompany/deleteCompany",{
				companyIds:delIds
			},function(data){
				data = JSON.parse(data);
				if(data.success){
					layer.msg("删除成功",{
						icon: 1,
						time: 2000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						loadContent("/operationcompany/companyList");
					});
				} else {
					layer.msg(data.msg,{
						icon: 2,
						time: 2000 //2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
		    layer.close(index);
		});
	}
}

//实现字段较长时的隐藏于显示
var index;
function mouseOverAttr(item , cont){
	index = layer.tips(cont, '#addr' + item, {
		  tips: [3, '#37CED4'],
		});
}

function mouseOutAttr(item , cont){
	layer.close(index);
}

var index1;
function mouseOverCont(item , cont){
	index1 = layer.tips(cont, '#cont' + item, {
		tips: [3, '#37CED4'],
	});
}

function mouseOutCont(item , cont){
	layer.close(index1);
}

//将选中项置顶
function updateSort(id){
	if(id != null){
   		$.get(ctx + "/operationcompany/updateSort",{
			id: id
   		},function(data){
   		data = JSON.parse(data);
		if(data.success){
			layer.msg("操作成功",{
			    icon: 1,
			    time: 2000 //2秒关闭（如果不配置，默认是3秒）
			},function(){
				loadContent("/operationcompany/companyList");
			});
		}else{
			layer.msg(data.msg,{
			    icon: 2,
			    time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	});
	}
}